<?php

namespace app\common\model\banxi\core;

use app\common\enums\AccountTypeEnum;
use app\common\enums\core\PayTypeEnum;
use app\common\enums\TcScheduleStatusEnum;
use app\common\enums\TrialClassOrderStatusEnum;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;

/**
 * 教练
 */
class CoreTrialClassOrder extends BaseModel
{

    use SoftDelete;

    protected $name = 'banxi_core_trial_class_order';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'orderStatusCn',
        'payTypeCn',
        'scheduleStatusCn',
        'createrCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'trainerId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getOrderStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['orderStatus'] ?? "");
        return (new TrialClassOrderStatusEnum())->getDescription($value) ?? "";
    }

    public function getPayTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['payType'] ?? "");
        return (new PayTypeEnum())->getDescription($value) ?? "";
    }

    public function getScheduleStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['scheduleStatus'] ?? "");
        return (new TcScheduleStatusEnum())->getDescription($value) ?? "";
    }

    public function getCreaterCnAttr($value, $data)
    {
        $value = $value ?: ($data['creater'] ?? "");
        return (new AccountTypeEnum())->getDescription($value) ?? "";
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field(['id', 'name']);
            }])->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit
            ]);
    }

    public function getInfo(array $where)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field(['id', 'name']);
            }])->where($where)->find();
    }

    public function countPayByStudent($studentId)
    {
        return $this->where('studentId', $studentId)
            ->whereNotIn('orderStatus', [
                TrialClassOrderStatusEnum::WAIT_PAY,
                TrialClassOrderStatusEnum::CANCEL,
                TrialClassOrderStatusEnum::FAIL_PAY
            ])
            ->count();
    }

    public function cancelByStudent($studentId)
    {
        return $this->where([
            ['studentId', '=', $studentId],
            ['orderStatus', 'in', [TrialClassOrderStatusEnum::WAIT_PAY]],
        ])->update(['orderStatus' => TrialClassOrderStatusEnum::CANCEL]);
    }

    public function queryOrderStatus($orderId)
    {
        $orderInfo = $this->where('id', $orderId)->find();
        if (empty($orderInfo)) {
            throw new ValidateException("订单不存在");
        }
        return $orderInfo->orderStatus == TrialClassOrderStatusEnum::SUCCESS_PAY;
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function trainer()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }
}